#include "playscene.h"
#include <QMenuBar>
#include <QPainter>
#include <mypushbutton.h>
#include <QTimer>
#include <QLabel>
#include "mycoin.h"
#include "dataconfig.h"
#include <QPropertyAnimation>
#include <QSoundEffect>

PlayScene::PlayScene(QWidget *parent)
    : QMainWindow{parent}
{}

PlayScene::PlayScene(int index)
{
    this->levelIndex = index;
    this->setFixedSize(320, 588);
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    this->setWindowTitle("翻金币");

    QMenuBar * bar = this->menuBar();
    this->setMenuBar(bar);
    QMenu * startMenu = bar->addMenu("开始");
    QAction * quitAction = startMenu->addAction("退出");
    connect(quitAction, &QAction::triggered, [=](){
        this->close();
    });

    //返回音效
    QSoundEffect * startSound = new QSoundEffect;
    startSound->setSource(QUrl::fromLocalFile(":/res/TapButtonSound.wav"));
    //翻金币音效
    QSoundEffect * flipSound = new QSoundEffect;
    flipSound->setSource(QUrl::fromLocalFile(":/res/ConFlipSound.wav"));
    //胜利音效
    QSoundEffect * winSound = new QSoundEffect;
    winSound->setSource(QUrl::fromLocalFile(":/res/LevelWinSound.wav"));

    MyPushButton * closeBtn = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");
    closeBtn->setParent(this);
    closeBtn->move(this->width()-closeBtn->width(), this->height()-closeBtn->height());
    connect(closeBtn, &MyPushButton::clicked, [=](){
        startSound->play();
        qDebug() << "cloBtn clicked";
        QTimer::singleShot(500, this, [=](){
            this->hide();
            emit this->chooseSceneBack();
        });
    });

    QLabel * label = new QLabel;
    label->setParent(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    label->setFont(font);
    QString str = QString("Level:%1"). arg(this->levelIndex);
    label->setText(str);
    label->setGeometry(QRect(30, this->height()-50, 120, 50));// 设置大小和位置

    //胜利的图片
    QLabel * winLabel = new QLabel;
    QPixmap tmpPix;
    tmpPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0, 0, tmpPix.width(), tmpPix.height());
    winLabel->setPixmap(tmpPix);
    winLabel->setParent(this);
    winLabel->move((this->width()-tmpPix.width())*0.5, -tmpPix.height());

    //创建金币的背景图片
    for(int i = 0; i< 4; i++)
    {
        for(int j=0; j<4; j++)
        {
            QLabel * label = new QLabel;
            label->setGeometry(0, 0, 50, 50);
            label->setPixmap(QPixmap(":/res/BoardNode(1).png"));
            label->setParent(this);
            label->move(57 + i*50,  200 + j * 50);

            MyCoin * coin = new MyCoin(":/res/Coin0001.png");
            coin->setParent(this);
            coin->move(59 + i * 50, 204 + j * 50);
        }
    }

    dataConfig config;
    for(int i = 0; i < 4; i ++)
    {
        for(int j = 0; j< 4;  j ++)
        {
            gameArray[i][j] = config.mData[this->levelIndex][i][j];
            QString img;
            if(gameArray[i][j] == 1)
            {
                img = ":/res/Coin0001.png";
            }
            else
            {
                img = ":/res/Coin0008.png";
            }
            MyCoin * coin = new MyCoin(img);
            coin->setParent(this);
            coin->move(59 + i * 50, 204 + j * 50);
            coin->poxX = i;
            coin->posY = j;
            coin->flag = gameArray[i][j];
            coinBtn[i][j] = coin;
            //监听点击，翻转金币
            connect(coin, &MyCoin::clicked, [=](){
                flipSound->play();
                coin->changeFlag();
                gameArray[i][j] = gameArray[i][j] == 0 ? 1 : 0;

                //翻转周围硬币
                QTimer::singleShot(300, this, [=](){
                    if(coin->poxX + 1 <=3)//右边
                    {
                        coinBtn[coin->poxX+1][coin->posY]->changeFlag();
                        gameArray[coin->poxX + 1][coin->posY] = gameArray[coin->poxX + 1][coin->posY] == 0? 1:0;
                    }
                    if(coin->poxX - 1 >= 0)//左边
                    {
                        coinBtn[coin->poxX-1][coin->posY]->changeFlag();
                        gameArray[coin->poxX - 1][coin->posY] = gameArray[coin->poxX - 1][coin->posY] == 0? 1:0;
                    }
                    if(coin->posY + 1 <=3)//下边
                    {
                        coinBtn[coin->poxX][coin->posY+1]->changeFlag();
                        gameArray[coin->poxX][coin->posY+1] = gameArray[coin->poxX][coin->posY+1] == 0? 1:0;
                    }
                    if(coin->posY - 1 >= 0)//上边
                    {
                        coinBtn[coin->poxX][coin->posY-1]->changeFlag();
                        gameArray[coin->poxX][coin->posY-1] = gameArray[coin->poxX][coin->posY-1] == 0? 1:0;
                    }

                    //判断是否胜利
                    this->isWin = true;
                    for(int i = 0; i<4; i++)
                    {
                        for(int j = 0; j < 4; j ++)
                        {
                            if(coinBtn[i][j] ->flag ==false)
                            {
                                this->isWin = false;
                                break;
                            }
                        }
                    }
                    if(this->isWin)
                    {
                        winSound->play();
                        qDebug() << "胜利" ;
                        QPropertyAnimation * animation1 = new QPropertyAnimation(winLabel, "geometry");
                        animation1->setDuration(1000);
                        animation1->setStartValue(QRect(winLabel->x(), winLabel->y(), winLabel->width(), winLabel->height()));
                        animation1->setEndValue(QRect(winLabel->x(), winLabel->y()+114, winLabel->width(), winLabel->height()));
                        animation1->setEasingCurve(QEasingCurve::OutBounce);
                        animation1->start();

                        //禁用所有按钮点击事件
                        for(int i = 0; i<4; i++)
                        {
                            for(int j = 0; j < 4; j ++)
                            {
                                coinBtn[i][j]->isWin = true;
                            }
                        }
                    }

                });

            });
        }
    }

}

void PlayScene::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0, 0, this->width(), this->height(), pix);

    pix.load(":/res/Title.png");
    pix = pix.scaled(pix.width()*0.5, pix.height()*0.5);
    painter.drawPixmap(10, 30, pix.width(), pix.height(), pix);
}

